package com.xiliu.server.modules.app.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiliu.common.result.R;
import com.xiliu.server.modules.app.vo.HouseDetailVo;
import com.xiliu.server.modules.app.vo.HouseVo;
import com.xiliu.server.modules.app.vo.SlideItemVo;
import com.xiliu.server.modules.basicdata.entity.XlCity;
import com.xiliu.server.modules.basicdata.entity.XlHouse;
import com.xiliu.server.modules.basicdata.entity.XlTopnav;
import com.xiliu.server.modules.basicdata.service.IXlCityService;
import com.xiliu.server.modules.basicdata.service.IXlHouseService;
import com.xiliu.server.modules.basicdata.service.IXlTopnavService;
import com.xiliu.server.modules.website.service.IXlSlideItemService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiliu
 * @date 2022/11/25 15:40
 */
@RestController
@RequestMapping("/mobile/home")
public class HouseIndexController {

    @Autowired
    private IXlCityService xlCityService;
    @Autowired
    private IXlTopnavService xlTopnavService;
    @Autowired
    private IXlHouseService xlHouseService;
    @Autowired
    private IXlSlideItemService xlSlideItemService;

    @ApiOperation("获取首页信息")
    @GetMapping("/index")
    public R index() {
        Map<String, Object> resultMap = new HashMap<>(3);
        // 首页弹窗广告
        List<SlideItemVo> popbanner = xlSlideItemService.getSlideItemByPosition("1");
        if (CollectionUtil.isNotEmpty(popbanner)) {
            Map<String, Object> popMap = new HashMap<>(3);
            popMap.put("image", popbanner.get(0).getImage());
            popMap.put("url", popbanner.get(0).getUrl());
            resultMap.put("popbanner", popMap);
        }
        // 首页顶部banner
        List<SlideItemVo> topbanner = xlSlideItemService.getSlideItemByPosition("2");
        resultMap.put("topbanner", topbanner);
        List<XlCity> cityList = xlCityService.list(new LambdaQueryWrapper<XlCity>()
                .eq(XlCity::getIsShow,1).orderByDesc(XlCity::getSortOrder));
        resultMap.put("city", cityList);
        // 导航
        List<XlTopnav> topnavList = xlTopnavService.list(new LambdaQueryWrapper<XlTopnav>()
                .eq(XlTopnav::getIsShow,1).orderByDesc(XlTopnav::getSortOrder));
        resultMap.put("topnav", topnavList);
        // 地图找房banner
        List<SlideItemVo> hotbanner = xlSlideItemService.getSlideItemByPosition("3");
        if (CollectionUtil.isNotEmpty(hotbanner)) {
            Map<String, Object> popMap = new HashMap<>(3);
            popMap.put("image", hotbanner.get(0).getImage());
            popMap.put("url", hotbanner.get(0).getUrl());
            resultMap.put("hotbanner", popMap);
        }
        // 我是业主banner
        List<SlideItemVo> yezhubanner = xlSlideItemService.getSlideItemByPosition("4");
        resultMap.put("yezhubanner", yezhubanner);
        // 品质公寓
        List<HouseVo> pzgyList = xlHouseService.getHouseListByType("pzgy");
        resultMap.put("pzgy", pzgyList);
        // 品质公寓banner
        List<SlideItemVo> pzgybanner = xlSlideItemService.getSlideItemByPosition("5");
        resultMap.put("pzgybanner", pzgybanner);
        // 整租
        List<HouseVo> zzList = xlHouseService.getHouseListByType("zz");
        resultMap.put("zhengzu", zzList);
        // 合租
        List<HouseVo> hzList = xlHouseService.getHouseListByType("hz");
        resultMap.put("hezu", hzList);
        // 短租
        List<HouseVo> dzList = xlHouseService.getHouseListByType("dz");
        resultMap.put("duanzu", dzList);
        // 商铺办公
        List<HouseVo> spbgList = xlHouseService.getHouseListByType("spbg");
        resultMap.put("spbg", spbgList);
        // 平台合作banner
        List<SlideItemVo> pthzbanner = xlSlideItemService.getSlideItemByPosition("6");
        resultMap.put("pthzbanner", pthzbanner);
        // 客服banner
        List<SlideItemVo> kfbanner = xlSlideItemService.getSlideItemByPosition("7");
        resultMap.put("kfbanner", kfbanner);
        return R.ok(resultMap);
    }

    @ApiOperation("获取房源详情信息")
    @GetMapping("/get-house-detail/{houseId}")
    public R getHgetHouseDetail(@PathVariable("houseId") Long houseId) {
        HouseDetailVo houseDetail = xlHouseService.getHouseDetailByHouseId(houseId);
        return R.ok(houseDetail);
    }
}